package se.peterbjorkman.android.trafikkamera;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import se.peterbjorkman.android.trafikkamera.providers.CameraContentProvider;
import se.peterbjorkman.android.trafikkamera.providers.CameraDefs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.Toast;

public class CameraGalleryActivity extends Activity {
	
	public static final String INTENT_EXTRA_ROUTEID = "routeid";
	
	private ArrayList<CameraGalleryActivity.CameraInfo> mInfos;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.gallery); // TODO remove layout? Only galler in it...
	    
	    long routeId = getIntent().getLongExtra(INTENT_EXTRA_ROUTEID, 0);
	    
		String[] projection = new String[] { 
				CameraDefs.NAME}; //TODO Cameradef

		Cursor nameCursor = managedQuery(
				CameraContentProvider.ROUTES_URI,
				projection, CameraDefs.ID + " = " + routeId, null, CameraDefs.NAME);
		String title = getTitle().toString();
		if (nameCursor.moveToFirst()){
			title += " - " + nameCursor.getString(0);
			setTitle(title);
		}
			
	    Gallery g = (Gallery) findViewById(R.id.gallery);

	    if (savedInstanceState != null)
	    	mInfos = (ArrayList<CameraInfo>) savedInstanceState.getSerializable("hådkodad"); // TODO hårdkodad key
	    if (mInfos == null){
	    	mInfos = extractCameraInfo(routeId);
	    	if (mInfos.size() <= 1){
	    		finish();
		    	if (mInfos.size() == 1){
		    		Context context = CameraGalleryActivity.this;
	                Intent launchIntent = new Intent(context,
	                        CameraPictureActivity.class);
	                launchIntent.putExtra(CameraPictureActivity.EXTRA_ID, mInfos.get(0).id);
	                context.startActivity(launchIntent);
		    	} else {
		    		Toast.makeText(this, "Inga kameror i rutten.", Toast.LENGTH_SHORT).show();
		    	}
		    		
		    	return;
	    	}
	    }
	    
	    g.setAdapter(new ImageDownloadingAdapter(this, mInfos));
	    Toast.makeText(this, "Svep med fingret för att växla kamera.", Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState){
		outState.putSerializable("hådkodad", mInfos);  // TODO hårdkodad key
	}
	
	private ArrayList<CameraInfo> extractCameraInfo(long routeId) {
		// TODO Duplicated code from RoutesCameraActivity.getChildFilter
		String filter = "EXISTS (SELECT 1 from camerasinroutes cir where cir.cameraid = cameras._id AND cir.routeId = " + routeId + ")";
		String[] projection = new String[] { 
				CameraDefs.ID, 
				CameraDefs.NAME,
				CameraDefs.DESCRIPTION,
				CameraDefs.CAMERA_URL };
		ArrayList<CameraInfo> infos = new ArrayList<CameraInfo>();		
		Cursor cur = managedQuery( // TODO Use managedQuey?
				CameraContentProvider.CONTENT_URI,
				projection,filter , null, null); // TODO Order
		while (cur.moveToNext()){
			CameraInfo info = new CameraInfo();
			info.id = cur.getLong(cur.getColumnIndexOrThrow(CameraDefs.ID));
			info.name = cur.getString(cur.getColumnIndexOrThrow(CameraDefs.NAME));
			info.description = cur.getString(cur.getColumnIndexOrThrow(CameraDefs.DESCRIPTION));
			info.url = cur.getString(cur.getColumnIndexOrThrow(CameraDefs.CAMERA_URL));
			infos.add(info);
		}

		cur.close(); // TODO use try finally? 
		return infos;
	}

	public class CameraInfo implements Serializable{
		private static final long serialVersionUID = -7932006788454919725L;
		public long id;
		public BitmapDrawable drawable;
		public String name;
		public String description;
		public String url;
		public Date lastModified;
	}
}
